﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace IveelySE.BigData.MasterNode
{
    class Program
    {
        /// <summary>
        /// 存储子节点列表
        /// </summary>
        public static StoreList storeList = new StoreList();
        /// <summary>
        /// 应用程序配置文件
        /// </summary>
        public static Config Config = new Config();
        /// <summary>
        /// 监听用户写线程
        /// </summary>
        private static Thread userWrite;
        /// <summary>
        /// 监听用户读线程
        /// </summary>
        private static Thread userRead;
        /// <summary>
        /// 监听存储子结点上线线程
        /// </summary>
        private static Thread onlineStore;

        static void Main(string[] args)
        {
            //执行初始化工作
            Init();
            //接受用户指令线程
            userWrite = new Thread(new ThreadStart(UserWrite));
            //启动用户线程
            userWrite.Start();
            Common.Printf.WriteInfo("用户指令线程启动完毕。");
            //用户读线程
            userRead = new Thread(new ThreadStart(UserRead));
            //启动线程
            userRead.Start();
            Common.Printf.WriteInfo("用户读线程启动完毕。");
            //接受存储结点上线线程
            onlineStore = new Thread(new ThreadStart(OnlineStoreNode));
            //启动监听上线线程
            onlineStore.Start();
            Common.Printf.WriteInfo("存储结点监听线程启动完毕。");
            //守护（reciveUser、onlineStore）线程
            Thread guardThread = new Thread(new ThreadStart(GuardThread));
            //启动守护线程
            guardThread.Start();
            Common.Printf.WriteInfo("守护线程启动完毕。");

        }

        /// <summary>
        /// 线程守护
        /// </summary>
       private static void GuardThread()
        {
            //不断执行
            while (true)
            {
                if (!userWrite.IsAlive)
                {
                    //接受用户指令线程
                    userWrite = new Thread(new ThreadStart(UserWrite));
                    //启动用户线程
                    userWrite.Start();
                }
                if (!onlineStore.IsAlive)
                {
                    //接受存储结点上线线程
                    onlineStore = new Thread(new ThreadStart(OnlineStoreNode));
                    //启动监听上线线程
                    onlineStore.Start();
                }

            }
        }
        /// <summary>
        /// 接受用户命令
        /// </summary>
        private static void UserWrite()
        {
            //开启监听
            UserCommand.Listen();
        }
        /// <summary>
        /// 用户读取数据
        /// </summary>
        private static void UserRead()
        {
            RemoteData remoteData = new RemoteData();
            remoteData.Listen();
        }
        /// <summary>
        /// 存储结点上线监听
        /// </summary>
        private static void OnlineStoreNode()
        {
            //开启监听
            storeList.Listen();
        }

        private static void Init()
        {
            Console.Write("是否修改配置文件？[y/n]");
            string modify = Console.ReadLine();
            if (modify.ToLower() == "y")
            {
                Console.WriteLine("\n 请按照; IP UserWritePort UserReadPort MasterReadPort MasterWritePort OnLineListenPort 输入:");
                string[] info = Console.ReadLine().Split(' ');
                Config.Modify(info);
            }
            else
            {
                Common.Printf.WriteInfo("用户取消修改配置文件。");
            }
           Common.Printf.WriteInfo( Config.ToString());
        }
    }
}
